#!/bin/bash


## load modules 
module load gcc/4.8.2 gdc samtools/1.9 java/1.8.0_73 picard-tools/2.20.2 sambamba/0.6.8

#information for each individual, respectively each bam file
ind=$LSB_JOBINDEX
short="DF$ind"
rg=$(echo "0$LSB_JOBINDEX")
library=$( echo "library$short")
lane=$( echo "1.$rg")

###Defining all the paths 
input="/path/to/input/bam"
output="/path/to/output/directory"
proc=1
##====================================================================================================
#Forward and reverse

picard FixMateInformation I=${input}/${short}_FR_n.bam O=/dev/stdout VALIDATION_STRINGENCY=LENIENT | picard AddOrReplaceReadGroups I=/dev/stdin O=${TMPDIR}/${short}_int1_n.bam RGID=${lane} RGlb=${library} RGPL=illumina RGSM=${rg} RGPU=unit1  
sambamba sort ${TMPDIR}/${short}_int1_n.bam -o ${TMPDIR}/${short}_FR_sorted_n.bam -t ${proc} -m 50GB --tmpdir ${TMPDIR}
picard MarkDuplicates TMP_DIR=${TMPDIR} INPUT=${TMPDIR}/${short}_FR_sorted_n.bam OUTPUT=${output}/${short}_FR_sorted_dup_WFRef_nova.bam METRICS_FILE=${output}/${short}_FR_duplication_metrics_WFRef.txt VALIDATION_STRINGENCY=LENIENT MAX_FILE_HANDLES_FOR_READ_ENDS_MAP=1024
sambamba index ${output}/${short}_FR_sorted_dup_WFRef_nova.bam
rm ${TMPDIR}/*

#Merged 
picard FixMateInformation I=${input}/${short}_M_n.bam O=/dev/stdout VALIDATION_STRINGENCY=LENIENT | picard AddOrReplaceReadGroups I=/dev/stdin O=${TMPDIR}/${short}_M_read_group_n.bam RGID=${lane} RGlb=${library} RGPL=illumina RGSM=${rg} RGPU=unit1 VALIDATION_STRINGENCY=LENIENT
sambamba sort ${TMPDIR}/${short}_M_read_group_n.bam -o ${TMPDIR}/${short}_M_sorted_n.bam -t ${proc} -m 50GB --tmpdir ${TMPDIR}
picard MarkDuplicates TMP_DIR=${TMPDIR} INPUT=${TMPDIR}/${short}_M_sorted_n.bam OUTPUT=${output}/${short}_M_sorted_dup_WFRef_nova.bam METRICS_FILE=${output}/${short}_M_duplication_metrics_WFRef.txt VALIDATION_STRINGENCY=LENIENT MAX_FILE_HANDLES_FOR_READ_ENDS_MAP=1024
sambamba index ${output}/${short}_M_sorted_dup_WFRef_nova.bam
##====================================================================================================

